package com.shopping.www.service;

import com.shopping.www.mapper.AdminMapper;
import com.shopping.www.model.Brand;
import com.shopping.www.model.RegisterBean;
import com.shopping.www.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.ArrayList;
import java.util.List;

public class AdminService {
    SqlSessionFactory factory= SqlSessionFactoryUtils.getSqlSessionFactory();
    public RegisterBean selectByInputBan(String username, String status){
        SqlSession sqlSession=factory.openSession();
        AdminMapper mapper=sqlSession.getMapper(AdminMapper.class);
        RegisterBean registerBean =mapper.selectByInputBan(username,status);
        sqlSession.close();
        return registerBean;
    }
    public void banBusiness(String username){
        SqlSession sqlSession=factory.openSession();
        AdminMapper mapper=sqlSession.getMapper(AdminMapper.class);
        mapper.banBusiness1(username);
        int id=mapper.selectBanBusiness(username);
        mapper.banBusiness2(id);
        sqlSession.commit();
        sqlSession.close();
    }
    public List<RegisterBean> selectBusinessBans(){
        SqlSession sqlSession=factory.openSession();
        AdminMapper mapper=sqlSession.getMapper(AdminMapper.class);
        String status="商家";
        List<RegisterBean> registerBeans =mapper.selectBusinessBans(status);
        sqlSession.close();
        return registerBeans;
    }
    public void liftBanBusiness(String username){
        SqlSession sqlSession=factory.openSession();
        AdminMapper mapper=sqlSession.getMapper(AdminMapper.class);
        mapper.liftBanBusiness1(username);
        int id=mapper.selectBanBusiness(username);
        mapper.liftBanBusiness2(id);
        sqlSession.commit();
        sqlSession.close();
    }
    public List<Brand> selectForBan(String businessName){
        SqlSession sqlSession=factory.openSession();
        AdminMapper mapper=sqlSession.getMapper(AdminMapper.class);
        int business_id=mapper.selectBusinessId(businessName);
        int[] ids=mapper.selectShelveIds(business_id);
        if(ids.length>0){
        List<Brand> brands=mapper.selectForBan(ids);
        sqlSession.close();
        return brands;
        }else{
            return new ArrayList<>();
        }
    }
    public boolean selectBanBrand(int id){
        SqlSession sqlSession=factory.openSession();
        AdminMapper mapper=sqlSession.getMapper(AdminMapper.class);
        int status=mapper.checkBanBrand(id);
        sqlSession.close();
        if(status==1)return true;
        else return false;
    }
    public String selectBanBrandApply(int brand_id){
        SqlSession sqlSession=factory.openSession();
        AdminMapper mapper=sqlSession.getMapper(AdminMapper.class);
        String apply=mapper.selectBanBrandApply(brand_id);
        return apply;
    }
    public void banBrand(Integer id){
        SqlSession sqlSession=factory.openSession();
        AdminMapper mapper=sqlSession.getMapper(AdminMapper.class);
        mapper.banBrand1(id);
        int shelveNum=mapper.shelveNum(id);
        int wareNum=mapper.wareNum(id);
        mapper.banBrand2(shelveNum,wareNum,id);
        sqlSession.commit();
        sqlSession.close();
    }
    public List<Brand> selectBanBrands(){
        SqlSession sqlSession=factory.openSession();
        AdminMapper mapper=sqlSession.getMapper(AdminMapper.class);
        int[] ban_ids=mapper.selectBan_id();
        int[] brand_ids=null;
        if(ban_ids.length>0) {
            brand_ids = mapper.selectBrand_id(ban_ids);
            List<Brand> brands = mapper.selectBanBrands(brand_ids);
            return brands;
        }
        if(ban_ids.length==0) {
            int[] p=new int[0];
            List<Brand> brands = mapper.selectBanBrands(p);
            return brands;
        }
        else {
            List<Brand> brandList=new ArrayList<>();
            return brandList;
        }
    }
    public void liftBanBrand(Integer id){
        SqlSession sqlSession=factory.openSession();
        AdminMapper mapper=sqlSession.getMapper(AdminMapper.class);
        mapper.liftBanBrand(id);
        sqlSession.commit();
        sqlSession.close();
    }

}
